<?php
namespace Push\Lib;
/**
 * 推送相关操作类
 * @author	Zhangwei	2017-06-13
 */
class PushLib {
	/**
	 * 发送推送消息
	 * @author  zhangwei   2017-06-13
	 * @param $title  		推送的标题(只支持安卓)
	 * @param $content  	推送的内容
	 * @param $extras 		扩展数据
	 * array('type'=>'system_message','event' => 'weibo_auth','notice_id' => 4,'status' => 1)
	 * 1. type 推送类型(必填数据) 1.1  type => 'weibo_at','weibo_comment','weibo_praise','system_message'
	 * 		weibo_at 微博@ | weibo_comment 微博评论 | weibo_praise 微博点赞 | system_message 系统消息
	 * 2. notice_id 小聚消息(必填数据)
	 * 3. event 推送id，张伟微博认证 (不必填，可自定义)
	 * 4. status 状态，张伟进行审核状态所用(不必填，可自定义)
	 * @param $uids  		用户id(为空则给全部用户发送)
	 */
	static public function push_message($title,$content,$extras,$uids =''){
		$params   = C('PUSH');
		$push_handle = new \PushMessage($params);
		if($uids){
			if(is_array($uids)){
				$receiver = array('alias' => $uids);
			}else{
				$receiver = array('alias' => explode(',',$uids));
			}
		}else{
			$receiver = 'all';
		};
		$push_res = $push_handle -> send_pub($title,$content,$extras,$receiver);
		$push_model = D('Push/Push');
		$data = array(
			'content' =>$content,
			'title'	  => $title,
			'receiver'=> is_array($uids) ? implode(',',$uids) : $uids,
			'type'	  =>$extras['type'],
			'time'	  =>time(),
			'result'  => $push_res['message']
		);
		$push_model -> add($data); //存储到数据库
		return $push_res;   //返回字段 ['message','error_code'];
	}
	
}
